#include "PlayStatusResMessage.h"
#include "jsonUtils.h"

PlayStatusResMessage::PlayStatusResMessage(MtpProtocol* packet) : MtpMessage(packet)
{
}

PlayStatusResMessage::PlayStatusResMessage(const int& version, const uint32_t& commandId, const int64_t& movieId, const int& scene,
	const int& duration, const int& position, const int& volume)
	: MtpMessage(new MtpProtocol(L"playStatusRes", version, commandId)), param(movieId, scene, duration, position, volume),res(true)
{
}

void PlayStatusResMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc& doc = res.jsonDeserialize(jsonContent);
		const JsonValue::ConstMemberIterator& it = doc.FindMember(L"data");
		if (it != doc.MemberEnd() && it->value.IsObject()) {
			param.jsonDeserialize(it->value);
		}
	}
}

void PlayStatusResMessage::encodeBody()
{
	JsonDoc& doc = res.jsonSerialize();
	JsonValue v(rapidjson::kObjectType);
	param.jsonSerialize(v, doc.GetAllocator());
	doc.AddMember(L"data", v, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}
